<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI面试官</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f4f4f9;
            color: #333;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .warning-counter {
            position: fixed;
            top: 20px;
            left: 20px;
            background: #ffc107;
            padding: 10px;
            border-radius: 4px;
            font-size: 18px; /* 增加字体大小 */
            font-weight: bold; /* 加粗字体 */
        }
        #preview {
            width: 300px;
            position: fixed;
            top: 20px;
            right: 20px;
            border: 2px solid #007bff;
            border-radius: 4px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .end-camera-button, .start-camera-button {
            margin-top: 20px;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .end-camera-button {
            background-color: red;
            color: white;
        }
        .start-camera-button {
            background-color: blue;
            color: white;
        }
        .hidden {
            display: none; /* 用于控制按钮显示 */
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI面试官</h1>
        <div class="warning-counter">警告次数：<span id="warningCount">0</span>/10</div>
        <button class="end-camera-button" id="endCameraButton">终止摄像</button>
        <button class="start-camera-button hidden" id="startCameraButton">开始摄像</button>
    </div>
    <video id="preview" autoplay muted playsinline></video>
    <script>
        // 防作弊相关变量
        let mediaStream = null;
        let warningCount = 0;
        let isInterviewEnded = false;
        let isCameraActive = true; // 摄像头是否活跃

        // 初始化媒体设备（仅开启摄像头）
        async function initializeMedia() {
            try {
                mediaStream = await navigator.mediaDevices.getUserMedia({
                    video: {
                        width: 800,
                        height: 600,
                        facingMode: 'user'  // 强制使用前置摄像头
                    }
                });

                const videoElement = document.getElementById('preview');
                videoElement.srcObject = mediaStream;
                videoElement.style.transform = 'scaleX(-1)'; // 添加镜像翻转

                return true;
            } catch (error) {
                alert('必须启用摄像头才能进行面试！');
                window.location.href = '/dashboard';
                return false;
            }
        }

        // 启动面试
        async function startInterview() {
            const mediaGranted = await initializeMedia();
            if (!mediaGranted) return;

            alert('请确保全程正面面对摄像头！');
        }

        // 切屏检测
        document.addEventListener('visibilitychange', () => {
            if (document.visibilityState === 'hidden' && !isInterviewEnded) {
                handleCheating('检测到切换屏幕！');
            }
        });

        // 监听窗口大小变化
        let lastWidth = window.innerWidth;
        let lastHeight = window.innerHeight;

        window.addEventListener('resize', () => {
            const currentWidth = window.innerWidth;
            const currentHeight = window.innerHeight;

            // 检查窗口大小，若变小到一定比例（例如小于原来的80%），则计数
            if (currentWidth < lastWidth * 0.8 || currentHeight < lastHeight * 0.8) {
                handleCheating('检测到窗口缩小，可能的切屏行为！');
            }
            lastWidth = currentWidth;
            lastHeight = currentHeight;
        });

        // 处理作弊检测
        function handleCheating(message) {
            warningCount++;
            document.getElementById('warningCount').textContent = warningCount;

            if (warningCount >= 10) {
                endInterview('检测到过多的切屏，面试结束！');
            } else {
                alert(message);
            }
        }

        // 结束面试
        function endInterview(message) {
            isInterviewEnded = true;
            alert(message);
            if (mediaStream) {
                mediaStream.getTracks().forEach(track => track.stop());
            }
            window.location.href = '/dashboard'; // 重定向到结束面试的页面
        }

        // 终止摄像按钮事件
        document.getElementById('endCameraButton').addEventListener('click', () => {
            const userConfirmed = confirm("结束摄像可能会导致终止面试，请确认是否结束摄像");
            if (userConfirmed) {
                if (mediaStream) {
                    mediaStream.getTracks().forEach(track => track.stop());
                    isCameraActive = false; // 摄像头已停用
                    document.getElementById('endCameraButton').classList.add('hidden'); // 隐藏停止按钮
                    document.getElementById('startCameraButton').classList.remove('hidden'); // 显示开始按钮
                    alert('摄像头已结束，仍在进行面试。');
                }
            }
        });

        // 开始摄像按钮事件
        document.getElementById('startCameraButton').addEventListener('click', async () => {
            if (!isCameraActive) {
                const mediaGranted = await initializeMedia();
                if (mediaGranted) {
                    isCameraActive = true; // 恢复摄像头
                    document.getElementById('startCameraButton').classList.add('hidden'); // 隐藏开始按钮
                    document.getElementById('endCameraButton').classList.remove('hidden'); // 显示停止按钮
                }
            }
        });

        // 启动面试
        startInterview();
    </script>
</body>
</html>